const axios = require('axios')
const cheerio = require('cheerio') 
const path = require('path')
const fs = require('fs')
const {searchDate,writeFiles,mkDir} = require('./util')

// 请求数据函数
function searChData(url,attr,doWork = null,currnPage=1,isJson = false) {
    return new Promise(function(resolve,reject) {
        axios.get(url).then(res => {
            const $ = cheerio.load(res.data,{decodeEntities: false})
            let aurl = $(attr)
            let arr = []
            switch(doWork) {
                case 'href': 
                    aurl.each((i,item) => {
                        let src = 'http://www.adoutu.com' + $(item).attr('href')
                        arr.push(src)
                    })
                    break;
                case 'src':
                    let pageNum = $('.article-pagination .pagination>li').length -2
                    aurl.each((i,item) => {
                        let src = $(item).attr('src')
                        let title = $(item).attr('title')
                        // 以json的格式存放数据
                        if (isJson) {
                            arr.push({
                                title,
                                src,
                                pageNum,
                                currnPage
                            })
                        } else {
                            arr.push({title,src})
                        }
                    })
                default:
                    aurl.each(async(i,e) => {
                        let itemDir = './Dir/' + $(e).text().trim()
                        arr.push($(e).text().trim())
                       await mkDir(itemDir)
                    })
            }
            resolve(arr)
        }).catch(err => {
            reject(err)
        })
    })
}

// 获取数据函数
async function getData() {
    let url = 'http://www.adoutu.com/index'
    let attrUrl = '.content-title .float-right'
    let attrDir = '.content-title .float-left'
    let urlData = await searChData(url,attrUrl,'href') // 请求获取分类的url数据
    let dirData = await searChData(url,attrDir) // 请求获取分类的数据

    saveData(dirData,urlData,false)
}

// 获取分页的数据函数
async function pagData(pageNum,pageUrl,attrUrl,isJson) {
    let arr = []
    if (!pageNum) {
        return
    }
    for(let i =0;i < pageNum; i++) {
        let num = i + 1
        pageUrl = pageUrl + num
       let data = await searChData(pageUrl,attrUrl,'src',i+2,isJson)
        data =  data.filter(item => {
            return item != ''
        })
        arr = arr.concat(data)
    }
    return arr
}


// 保存数据函数
function saveData(dirData,urlData,isJson = false) {
    let attrUrl = '.picture-list a>img'
    dirData.forEach(async(dirDataItem,index) => {
     // 最后一个参数值是判断用的是json格式存储，还是直接存储图片
    let data = urlData[index] && await searChData(urlData[index],attrUrl,'src',isJson)
    let pageUrl = urlData[index] &&　urlData[index].substring(0,urlData[index].length-1) 
    let leftPageData =data[0] && await pagData(data[0].pageNum-1,pageUrl,attrUrl,isJson)
    data =  data.filter(item => {
        return item != ''
    })
    data = data.concat(leftPageData)
    if (isJson) {
        let dir = './Dir/' + dirDataItem + '/imgs.json'
      // 调用写入函数
      await writeFiles(dir,JSON.stringify(data))
    } else {
        data.forEach(async (item,index) => {
            if (item) {
                let imgPath = `./Dir/${dirDataItem}/${dirDataItem}-${index+1}${path.extname(item.src)}`
               let ws = fs.createWriteStream(imgPath)
                axios.get(item.src,{responseType:"stream"}).then(res => {
                    res.data.pipe(ws)
                    res.data.on('close',()=> {
                        console.log(`${item.src}写入完成`)
                        ws.close()
                    })
                }).catch(err => {
                    console.log('写入失败：',err)
                })
            }
        })
    } 
    });
}
getData()




